package com.example.shoppingmallspringboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.shoppingmallspringboot.entity.UniAppUser;
import com.example.shoppingmallspringboot.entity.UniLoginDB;
import com.example.shoppingmallspringboot.service.UniUserService;
import com.example.shoppingmallspringboot.util.FwResult;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.annotations.Operation;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class UniUserController {
    @Autowired
    UniUserService uniUserService;
    @PostMapping("/uniAppLogin")
    @Operation(summary = "小程序登录获取openid",description = "code为前端获取到的值")
    public FwResult uniAppLogin(@RequestBody List<UniLoginDB> uniLoginDB) throws IOException {
        String code = uniLoginDB.get(0).getCode();
        String nickName = uniLoginDB.get(0).getUserName();
        String avatarUrl = uniLoginDB.get(0).getAvatarUrl();
        String appid = "wx2b17259c223d1ab4";
        String secret = "0d3cf8545e27aa69661fb7aff88da6ee";
//        String appid = "wx164751ad52c2347d";
//        String secret = "35356f55f20189b4d2ae5c875a5e2196";
        String openid = null;
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code=" + code + "&grant_type=authorization_code ";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        UniAppUser uniAppUser = new UniAppUser();
        if (response.isSuccessful()) {
            JSONObject jsonObject = JSON.parseObject(response.body().string());
            openid = (String) jsonObject.get("openid");
            Integer uuid= UUID.randomUUID().toString().replaceAll("-","").hashCode();
            uuid = uuid < 0 ? -uuid : uuid;//String.hashCode() 值会为空
            uniAppUser.setId(uuid);
            uniAppUser.setAvatarUrl(avatarUrl);
            uniAppUser.setNickName(nickName);
            uniAppUser.setOpenId(openid);
            return uniUserService.UniLogin(uniAppUser);
        }else {
            return FwResult.ok();
        }

    }
    @PostMapping("/uniApp/updateAvatarUrl")
    @Operation(summary = "用户修改头像",description = "UniAppUser为参数")
    public FwResult updateAvatarUrl(@RequestBody UniAppUser uniAppUser){
        System.out.println(uniAppUser);
        return uniUserService.updateAvatarUrl(uniAppUser);
    }
    @PostMapping("/uniApp/updateNickName")
    @Operation(summary = "用户修改头像",description = "UniAppUser为参数")
    public FwResult updateNickName(@RequestBody UniAppUser uniAppUser){
        return uniUserService.updateNickName(uniAppUser);
    }
}
